﻿using MediatR;
using System.Linq;
using SystemWFH.API.Application.Command.WorkshopCommand;
using SystemWFH.Infrstructer.Interface;

namespace SystemWFH.API.Application.CommandHandlers.WorkshopCommandHandler
{
    /// <summary>
    /// 批量删除
    /// </summary>
    public class WorkshopDeleteCommandHandler : IRequestHandler<WorkshopDeleteCommand, int>
    {
        private readonly IWorkshopRepository workshopRepository;
        public WorkshopDeleteCommandHandler(IWorkshopRepository workshopRepository)
        {
            this.workshopRepository = workshopRepository;
        }

        public async Task<int> Handle(WorkshopDeleteCommand request, CancellationToken cancellationToken)
        {
            //if (request.workshopId.Length == 0)
            //{
            //    return 0;
            //}
            //string[] arrer=request.workshopId.Split(',');
            //var str = await workshopRepository.GetAsync(x => arrer.Contains((x.workshopId).ToString()));
            //str.ForEach(x =>
            //{
            //    x.DelectName = request.DelectName;
            //    x.DelectTime = DateTime.Now.ToUniversalTime();
            //    x.IsDelete = true;
            //});
            //var list = await workshopRepository.GetDelete(str);
            //return list;
            var list = (await workshopRepository.GetAll()).Where(x => request.workshopId.Contains(x.workshopId)).ToList();
            foreach (var item in list)
            {
                item.IsDelete = true;
                await workshopRepository.Update(item);
            }
            return 1;
        }
    }
}
